﻿namespace Web.Control.Member.My
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Model.WWW_User.Teach;
    using BLF.WWW_User.Teach;
    using Lx.Web.Common.ControlsUI;
    using System.Collections.Generic;
    using System.Collections;
    using System.Text;
    public class Mysessions : BaseControl
    {
        protected string Memberno = "";
        /// <summary>
        /// 页面
        /// </summary>
        protected int pageIndex = 1;
        protected Literal lLCount;
        /// <summary>
        /// 数据数量st,gtype,p,ll
        /// </summary>
        protected int RecordCount = 0;
        protected List<SessionsModel> smlist = new List<SessionsModel>();
        protected List<SessionsModel> smlist1 = new List<SessionsModel>();
        /// <summary>
        /// 要操作的
        /// </summary>
        protected Repeater rpsm1;
        /// <summary>
        /// 其他的
        /// </summary>
        protected Repeater rpsm2;
        protected string Timezone = "0";
        protected string tp = "1";
        protected string TP350 = "";
        protected string TP351 = "";
        protected string TP352 = "";
        protected string TP353 = "";
        protected string TP354 = "";
        protected string TP355 = "";
        protected string TP356 = "";
        protected string TP357 = "";
        protected string TP358 = "";
        protected string TP359 = "";
        protected string TP360 = "";
        protected string TP361 = "";

        protected string TP250 = "";  
     

        protected string TP112="";
        protected string TP113="";
        protected string TP137="";
        protected string TP138="";
        protected string TP139="";
        protected string TP140="";
        protected string TP141="";
        protected string TP142="";
        protected string TP143="";
        protected string TP144="";
        protected string TP145="";
        protected string TP136 = "";
        protected string TP946 = "";
        protected string TP954 = "";
        protected string TP950 = "";
        protected string TP953 = "";
        protected string TP952 = "";
        protected string TP951 = "";
        protected string TP949 = "";
        protected string C0033 = "";
        protected int LUCount = 0;
        protected int CMCount = 0;
        protected int CCCount = 0;
        protected int RECount = 0;
        protected string MyRSession = "";
        protected string ORSession = "";
        protected System.Globalization.CultureInfo CurLan = new System.Globalization.CultureInfo(Lx.Web.Common.UI.Globals.Language);
        protected string PayDetail = System.Configuration.ConfigurationManager.AppSettings["PayDetail"];
        protected string imgResponse = "icon_response";
        protected override void OnLoad(EventArgs e)
        {
            Memberno = HttpContext.Current.User.Identity.Name;
            ArrayList arrylist = new ArrayList();
            if (Memberno == "")
            {
                Page.Response.Redirect("/user/login.htm?ReturnUrl=" + Page.Request.Url.AbsoluteUri);
                return;
            }
            arrylist = Teach.GetMyHdSessions(Memberno);
            if (arrylist != null && arrylist.Count > 0)
            {
                TP250 = GetText("TP250");
                TP350 = GetText("TP350");
                TP351 = GetText("TP351");
                TP352 = GetText("TP352");
                TP353 = GetText("TP353");
                TP354 = GetText("TP354");
                TP136 = GetText("TP136");
                TP355 = GetText("TP355");
                TP356 = GetText("TP356");
                TP357 = GetText("TP357");
                TP358 = GetText("TP358");
                TP359 = GetText("TP359");
                TP360 = GetText("TP360");
                TP361 = GetText("TP361");
                C0033 = GetText("C0033");

                TP112 = GetText("TP112");
                TP113 = GetText("TP113");
                TP137 = GetText("TP137");
                TP138 = GetText("TP138");
                TP139 = GetText("TP139");
                TP140 = GetText("TP140");
                TP141 = GetText("TP141");
                TP142 = GetText("TP142");
                TP144 = GetText("TP144");
                TP145 = GetText("TP145");
                TP143 = GetText("TP143");
                TP946 = GetText("TP946");
                TP954 = GetText("TP954");
                TP950 = GetText("TP950");
                TP953 = GetText("TP953");
                TP952 = GetText("TP952");
                TP951 = GetText("TP951");
                TP949 = GetText("TP949");
                smlist = arrylist[0] as List<SessionsModel>;
                smlist1 = arrylist[1] as List<SessionsModel>;
                Timezone = (string)arrylist[2];
                LUCount = (int)arrylist[3];
                CMCount = (int)arrylist[4];
                CCCount = (int)arrylist[5];
                 MyRSession = GetMyRSession();
                 ORSession = GetORSession();
            }
            else {
                Response.End();
            }
            base.OnLoad(e);
        }
        protected string GetStateText(string state,string StartDate, string StartTime, short LongTime, DateTime dt1, DateTime dt2)
        {
            string va = "";
            switch (state)
            {
                case "1":                       
                case "2":
                case "4":
                case "9":
                case "A":
                case "D":
                    va = TP354;
                    break;
                case "E":
                case "F":
                case "G":
                    if (dt2.Subtract(dt1).TotalDays > 7)
                    {
                        va = TP353;
                    }
                    else
                    {
                        va = TP352;
                    }
                    break;
                case "6":
                    try
                    {
                        DateTime dateTime = new DateTime(int.Parse(StartDate.Substring(0, 4)), int.Parse(StartDate.Substring(4, 2)), int.Parse(StartDate.Substring(6, 2)));
                        double h = 0;
                        double m = 0;
                        string[] strSp = StartTime.Split(':');
                        if (strSp.Length > 1)
                        {
                            double.TryParse(strSp[0], out h);
                            double.TryParse(strSp[1], out m);
                        }
                        dateTime = dateTime.AddMinutes(h * 60 + m);
                        DateTime datetime1 = dateTime.AddMinutes((double)(LongTime * 30));
                        DateTime datetime2 = DateTime.UtcNow;
                        if (dateTime <= datetime2 && datetime1 >= datetime2)
                        {
                            va = TP350;
                        }
                        else
                        {
                            va = TP351;
                        }
                    }
                    catch
                    {
                        va = TP351;
                    }
                    break;
                case "0": va = TP355;
                    break;
                case "5": va = TP356;
                    break;
                case "3": va = TP357;
                    break;
                case "7": va = TP358;
                    break;
                case "B": va = TP359;
                    break;
                case "8": va = TP360;
                    break;
                case "C": va = TP361;
                    break;
                case "H": va = TP361;
                    break;

            }
            return va;
        }
        protected string GetImg(string StartDate, string StartTime, short LongTime)
        {
                try
                {
                    DateTime dateTime = new DateTime(int.Parse(StartDate.Substring(0, 4)), int.Parse(StartDate.Substring(4, 2)), int.Parse(StartDate.Substring(6, 2)));
                    double h = 0;
                    double m = 0;
                    string[] strSp = StartTime.Split(':');
                    if (strSp.Length > 1)
                    {
                        double.TryParse(strSp[0], out h);
                        double.TryParse(strSp[1], out m);
                    }
                    dateTime = dateTime.AddMinutes(h * 60 + m);
                    DateTime datetime1 = dateTime.AddMinutes((double)(LongTime * 30));
                    DateTime datetime2 = DateTime.UtcNow;
                    if (dateTime <= datetime2 && datetime1 >= datetime2)
                    {
                        return "<img src='/images/icon_live.gif' align='absmiddle' title='[LIVE]' />";
                    }
                    else
                    {
                        return "<img scr='/Images/icon_upcoming.gif' align='absmiddle' title='[UPCOMING]'/>";
                    }
                }
                catch
                {
                    return "<img scr='/Images/icon_upcoming.gif' align='absmiddle' title='[UPCOMING]'/>";
                }
        }
        /// <summary>
        /// 格式化时间间隔值
        /// </summary>
        /// <param name="format">时间格式，例：years:months:days:hours:minutes:seconds</param>
        /// <param name="bDt">大时间</param>
        /// <param name="sdt">小时间</param>
        /// <returns>返回格式后两个时间间隔文本</returns>
        private String GetDateTimeFormat(String strFormatText, DateTime bDt, DateTime sdt)
        {
            TimeSpan ts = bDt - sdt;
            string strDiffdate = string.Format("{0}:{1}:{2}:{3}:{4}:{5}",
             (ts.TotalDays < 365 ? "0" : ((int)(ts.TotalDays / 365)).ToString()),
             (ts.TotalDays < 30 ? "0" : ((int)(ts.TotalDays / 30)).ToString()),
             (ts.TotalDays < 1 ? "0" : ((int)ts.TotalDays).ToString()),
             (ts.TotalHours < 1 ? "0" : ((int)ts.TotalHours).ToString()),
             (ts.TotalMinutes < 0 ? "0" : ((int)ts.TotalMinutes).ToString()),
             (ts.TotalSeconds < 0 ? "0" : ((int)ts.TotalSeconds).ToString()));

            string[] a = C0033.Split(':');
            string[] b = strDiffdate.Split(':');
            if (a.Length < 6 || b.Length < 6) return strFormatText;
            if (b[0] != "0")
            {
                return strFormatText.Replace("{0}", b[0]).Replace("{1}", a[0]);
            }
            else if (b[1] != "0")
            {
                return strFormatText.Replace("{0}", b[1]).Replace("{1}", a[1]);
            }
            else if (b[2] != "0")
            {
                return strFormatText.Replace("{0}", b[2]).Replace("{1}", a[2]);
            }
            else if (b[3] != "0")
            {
                return strFormatText.Replace("{0}", b[3]).Replace("{1}", a[3]);
            }
            else if (b[4] != "0")
            {
                return strFormatText.Replace("{0}", b[4]).Replace("{1}", a[4]);
            }
            else if (b[5] != "0")
            {
                return strFormatText.Replace("{0}", b[5]).Replace("{1}", a[5]);
            }
            else
            {
                return "";
            }
        }
        private int sortCompar(SessionsModel sm1, SessionsModel sm2)
        {
            return sm1.CountMsg.CompareTo(sm2.CountMsg);
        }
        /// <summary>
        /// 显示我要操作的session
        /// </summary>
        /// <returns>string</returns>
        protected string GetMyRSession()
        {
            //我为学生等待我操作的
            List<SessionsModel> sessionS = new List<SessionsModel>();
            //我为老师等待我操作的
            List<SessionsModel> sessionT = new List<SessionsModel>();
            DateTime CurDTime = DateTime.Now;
            //0:图片名称、1会员号、2是否教师、3昵称、4标题、5时间提示、6操作
            string concent = "<li class='right_tp_ul'><div class='floatl' style='width:20px'><img src='/Images/{0}.gif'/></div><div class='floatl'> <span class='text14'><a href='/{1}{2}.htm'>{3}</a> - {4}</span><p>{7}</p><p><ul class='sortby'><li class='text'>{5}</li>{6}</ul></p></div><div class='clear'></div></li>";
            string hrUrl = "";//第一个操作连接
            string hrView = "";//查看session连接 
            foreach (SessionsModel sm in smlist)
            {
                hrView = string.Format("<li class='last'><a href='/session/session.htm?sid={0}'>{1}</a></li>", sm.SessionID, TP136);
                string title = sm.SessionType == "1" ? string.Format("<a href='/lesson/" + sm.LID.ToString() + "'>" + GetText("TP406", new string[] { sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle) }) + "</a>") : TP250;
                DateTime dt1 = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, "480");
                string staDate = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).ToString(TimeToString1, CurLan) + "-" + BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).AddMinutes((double)((sm.LongTime * 30))).ToString("HH:mm");
                switch (sm.State)
                {
                    case "5"://付款
                        
                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            hrUrl = string.Format("<li><a href='{0}'>{1}</a></li>{2}", string.Format(PayDetail, sm.SessionID), TP141, hrView);
                            if (CurDTime.AddHours(24) < dt1)
                                dt1 = sm.HappenDate.AddHours(24);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP946, dt1, CurDTime), hrUrl, staDate);
                            sessionS.Add(sm);
                        }
                        break;
                    case "0":
                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            hrUrl = string.Format("<li><a href='/session/sessionstate3.htm?sid={0}'>{2}</a></li><li><a href='#' onclick='fctState({0},\"4\")'>{3}</a></li>{1}", sm.SessionID, hrView, TP137, TP138);
                            if (CurDTime.AddDays(7) < dt1)
                                dt1 = sm.HappenDate.AddDays(7);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP953, dt1, CurDTime), hrUrl, staDate);
                            sessionT.Add(sm); 
                        }
                        break;
                    case "3":
                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            hrUrl = string.Format("<li><a href='/session/sessionstate0.htm?sid={0}'>{2}</a></li><li><a href='#' onclick='fctState({0},\"1\")'>{3}</a></li>{1}", sm.SessionID, hrView, TP139, TP140);
                            if (CurDTime.AddDays(7) < dt1)
                                dt1 = sm.HappenDate.AddDays(7);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP954, dt1, CurDTime), hrUrl, staDate);
                            sessionS.Add(sm);
                        }
                        break;
                    case "7"://课时已结束，老师还未确认
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(3)-CurDTime)).TotalMinutes;
                        hrUrl = string.Format("<li><a href='/session/sessionstateb.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstate8.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, TP142, TP143, hrView);
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP949, sm.HappenDate.AddDays(3), CurDTime), hrUrl, staDate);
                        sessionT.Add(sm);
                        break;
                    case "B"://学生还未确认课时,老师已确认上完课
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7)-CurDTime)).TotalMinutes;
                        hrUrl = string.Format("<li><a href='/session/sessionstatef.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstatec.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, TP142, TP144, hrView);
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP950, sm.HappenDate.AddDays(7), CurDTime), hrUrl, staDate);
                        sessionS.Add(sm);
                        break;
                    case "C"://学生申请退款 老师解释
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(14)-CurDTime)).TotalMinutes;
                        hrUrl = string.Format("<li><a href='/session/sessionstateh.htm?sid={0}'>{1}</a></li>{2}", sm.SessionID, TP145, hrView);
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP951, sm.HappenDate.AddDays(14), CurDTime), hrUrl, staDate);
                        sessionT.Add(sm);
                        break;
                    case "8"://老师声明此课时无效,学生是否同意
                        hrUrl = string.Format("<li><a href='/session/sessionstate9a.htm?sid={0}'>{1}</a></li><li><a href='/session/sessionstate9a.htm?sid={0}'>{2}</a></li>{3}", sm.SessionID, TP112, TP113, hrView);
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7)-CurDTime)).TotalMinutes;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP952, sm.HappenDate.AddDays(7), CurDTime), hrUrl, staDate);
                        sessionS.Add(sm);
                        break;
                }
            }
            sessionS.Sort(sortCompar);
            sessionT.Sort(sortCompar);
            sessionT.AddRange(sessionS);
            StringBuilder strAppend = new StringBuilder();
            RECount = sessionT.Count + RECount;
            foreach (SessionsModel sm in sessionT)
            {
                strAppend.Append(sm.BaseLanguage);
            }
            return strAppend.ToString();
        }

        protected string GetORSession()
        {
              //我为老师的时候等待学生操作
             List<SessionsModel> sessionS = new List<SessionsModel>();
            //我为学生的时候等待老师操作
             List<SessionsModel> sessionT = new List<SessionsModel>();
            string TP924 = GetText("TP924");
            DateTime CurDTime = DateTime.Now;
            //0:图片名称、1会员号、2是否教师、3昵称、4标题、5时间提示、6操作
            string concent = "<li class='right_tp_ul'><div class='floatl' style='width:20px'><img src='/Images/{0}.gif'/></div><div class='floatl'> <span class='text14'><a href='/{1}{2}.htm'>{3}</a> - {4}</span><p>{7}</p><p><ul class='sortby'><li class='text'>{5}</li>{6}</ul></p></div><div class='clear'></div></li>";
            string hrView = "";//查看session连接 
            foreach (SessionsModel sm in smlist1)
            {
                hrView = string.Format("<li class='last'><a href='/session/session.htm?sid={0}'>{1}</a></li>", sm.SessionID, TP136);
                string title = sm.SessionType == "1" ? string.Format("<a href='/lesson/" + sm.LID.ToString() + "'>" + GetText("TP406", new string[] { sm.LID.ToString(), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.CTitle), Lx.Web.Common.UI.Globals.HtmlStrippedText(sm.LTitle) }) + "</a>") : TP250;
                DateTime dt1 = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, "480");
                string staDate = BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).ToString(TimeToString1, CurLan) + "-" + BLF.WWW_User.Teach.Teach.ConvertTime(sm.StartDate, sm.StartTime, Timezone).AddMinutes((double)((sm.LongTime * 30))).ToString("HH:mm");
                switch (sm.State)
                {
                    case "5"://付款

                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            if (CurDTime.AddHours(24) < dt1)
                                dt1 = sm.HappenDate.AddHours(24);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP946, dt1, CurDTime), hrView, staDate);
                            sessionS.Add(sm);
                        }
                        break;
                    case "0":
                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            if (CurDTime.AddDays(7) < dt1)
                                dt1 = sm.HappenDate.AddDays(7);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP953, dt1, CurDTime), hrView, staDate);
                            sessionT.Add(sm);
                        }
                        break;
                    case "3":
                        if (dt1 > CurDTime)
                        {//icon_response.gif
                            if (CurDTime.AddDays(7) < dt1)
                                dt1 = sm.HappenDate.AddDays(7);
                            sm.CountMsg = (int)((TimeSpan)(dt1 - CurDTime)).TotalMinutes;
                            sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP954, dt1, CurDTime), hrView, staDate);
                            sessionS.Add(sm);
                        }
                        break;
                    case "7"://课时已结束，老师还未确认
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(3) - CurDTime)).TotalMinutes;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP949, sm.HappenDate.AddDays(3), CurDTime), hrView, staDate);
                        sessionT.Add(sm);
                        break;
                    case "B"://学生还未确认课时,老师已确认上完课
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7) - CurDTime)).TotalMinutes;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP950, sm.HappenDate.AddDays(7), CurDTime), hrView, staDate);
                        sessionS.Add(sm);
                        break;
                    case "C"://学生申请退款 老师解释
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(14) - CurDTime)).TotalMinutes;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO, "T", sm.TNickname, title, GetDateTimeFormat(TP951, sm.HappenDate.AddDays(14), CurDTime), hrView, staDate);
                        sessionT.Add(sm);
                        break;
                    case "8"://老师声明此课时无效,学生是否同意
                        sm.CountMsg = (int)((TimeSpan)(sm.HappenDate.AddDays(7) - CurDTime)).TotalMinutes;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.SNO, "", sm.SNickname, title, GetDateTimeFormat(TP952, sm.HappenDate.AddDays(7), CurDTime), hrView, staDate);
                        sessionS.Add(sm);
                        break;
                    case "H"://老师声明此课时无效,学生是否同意
                        sm.CountMsg = 0;
                        sm.BaseLanguage = string.Format(concent, imgResponse, sm.TNO == Memberno ? sm.SNO : sm.TNO, sm.TNO == Memberno ? "" : "T", sm.TNO == Memberno ? sm.SNickname : sm.TNickname, title, TP924, hrView, staDate);
                        sessionS.Add(sm);
                        break;
                }
            }
            sessionS.Sort(sortCompar);
            sessionT.Sort(sortCompar);
            sessionS.AddRange(sessionT);
            StringBuilder strAppend = new StringBuilder();
            RECount = sessionS.Count + RECount;
            foreach (SessionsModel sm in sessionS)
            {
                strAppend.Append(sm.BaseLanguage);
            }
            return strAppend.ToString();
        }
    }
}